package com.lyj.mall.service.impl;


import com.lyj.mall.dao.UserMapper;
import com.lyj.mall.pojo.User;
import com.lyj.mall.service.UserService;
import com.lyj.mall.vo.ResponseVo;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;

import static com.lyj.mall.enums.ResponseEnum.ERROR;
import static com.lyj.mall.enums.ResponseEnum.USER_EXIT;

/**
 * @author lyj
 * @date 2019/12/17 23:33
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(User record) {
        return userMapper.insert(record);
    }

    @Override
    public ResponseVo register(User user) {
        User userByUsername = userMapper.findUserByUsername(user.getUsername());
        if (null != userByUsername) {
            return ResponseVo.error(USER_EXIT);
        }
        // md5摘要算法
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes(StandardCharsets.UTF_8)));
        int result = userMapper.insertSelective(user);
        if (result <= 0) {
            return ResponseVo.error(ERROR);
        }

        return ResponseVo.success();
    }

    @Override
    public User selectByPrimaryKey(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(User record) {
        return userMapper.updateByPrimaryKey(record);
    }

    @Override
    public ResponseVo<User> login(String username, String password) {

        return null;
    }

    @Override
    public User selectUserByUsername(String username) {
        return userMapper.findUserByUsername(username);
    }

}
